// 导入 path内置模块
const { resolveCname } = require("dns");
const path = require("path");
const db = require("../db/index");
module.exports.addArticle = (req, res) => {
  if (!req.file || req.file.fieldname !== "cover_img") return res.cc("文章封面是必选参数！");
  // TODO 有数据就将数据写入到数据库中
  // 构建数据对象
  const article_data = {
    ...req.body,
    pub_date: new Date(),
    author_id: req.user.id,
    cover_img: path.join("/uploads", req.file.filename)
  };

  // 构建 sql语句
  const sql = "insert into ev_articles set ?";

  // 执行sql语句
  db.query(sql, article_data, (err, data) => {
    if (err) return res.cc(err);
    if (data.affectedRows !== 1) return res.cc("文章添加失败");
    res.cc("发布文章成功", 0);
  });
};
